/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on Oct 21, 2010, 10:43:44 PM
 */

package lmclogistics;

import components.NButton;
import components.Lightbox;
import components.Image;
import components.Button;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import sql.Sql;



/**
 *
 * @author Sonia Tan
 */
public class Login extends javax.swing.JFrame {

    /** Creates new form Login */

    private Image bg;
    private Button button[] = new Button[8];
    private Button maintenanceSubmenu[] = new Button[15];
    private JLayeredPane panel;
    private static int i = 0,j = 0;
    private JTextField usernameField = new JTextField();
    private JPasswordField passwordField = new JPasswordField();
    private static JLabel description = new JLabel();
    private JPanel pl;
    private JButton logout = new JButton("Log out");
    private JLabel username = new JLabel();
    private JLabel password = new JLabel();
    private JButton login = new JButton();
    private NButton cancel = new NButton("Cancel",370,364);
    private static int accessType;

    public Login() {       
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("LMC Logistics");
        setVisible(true);
        setSize(919,577);
        centerScreen();
        setResizable(false);

        BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("/images/favicon.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setIconImage(image);

        
        panel = new JLayeredPane();
        panel.setSize(919,577);
        panel.setLayout(null);
        add(panel);

        setBg();
        login();
        initText();
        
    }

    public final void setBg(){
        bg = new Image("bg.jpg",919,577);
        panel.add(bg);
        panel.setLayer(bg,-1,-1);
        bg.setLocation(0,0);

        
    }

    public void setButton(String button,String rollover,int x, int y){
        this.button[i] = new Button(button,130,130);
        this.button[i].setLocation(x,y);
        this.button[i].setRollover(rollover);
        panel.add(this.button[i]);
        panel.setLayer(this.button[i], 0);
        i++;

    }

    public void loginSuccessful(){
        if(accessType == 0){
            panel.remove(login);
            panel.remove(cancel);
            panel.remove(username);
            panel.remove(password);
            panel.remove(usernameField);
            panel.remove(passwordField);

            setButton("maintenance button.png","maintenance button selected.png",81,209);
            setButton("summary button.png","summary button selected.png",206,209);
            setButton("billing button.png","billing button selected.png",331,209);
            setButton("user button.png","user button selected.png",456,209);

            setButton("eir out button.png","eir out button selected.png",81,335);
            setButton("eir in button.png","eir in button selected.png",206,335);
            setButton("outstanding chassis client button.png","outstanding chassis client button selected.png",331,335);
            setButton("outstanding chassis button.png","outstanding chassis button selected.png",456,335);

            buttonListeners();

            logout.setLocation(500,510);
            logout.setSize(80,25);
            logout.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                        Login out = new Login();
                        i = 0;
                        dispose();
                }
            });

            panel.add(logout,0,1);

            NotifyStock notify = new NotifyStock();
        }else if (accessType == 1){
            panel.remove(login);
            panel.remove(cancel);
            panel.remove(username);
            panel.remove(password);
            panel.remove(usernameField);
            panel.remove(passwordField);

            setButton("maintenance button.png","maintenance button selected.png",81,209);
            setButton("summary button.png","summary button selected.png",206,209);
            setButton("billing button.png","billing button selected.png",331,209);
            setButton("user button.png","user button selected.png",456,209);

            setButton("eir out button.png","eir out button selected.png",81,335);
            setButton("eir in button.png","eir in button selected.png",206,335);
            setButton("outstanding chassis client button.png","outstanding chassis client button selected.png",331,335);
            setButton("outstanding chassis button.png","outstanding chassis button selected.png",456,335);

            buttonListeners();

            logout.setLocation(500,510);
            logout.setSize(80,25);
            logout.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                        Login out = new Login();
                        i = 0;
                        dispose();
                }
            });

            panel.add(logout,0,1);

            disableButtons();
            enableButtons();

        }else if (accessType == 2){
            panel.remove(login);
            panel.remove(cancel);
            panel.remove(username);
            panel.remove(password);
            panel.remove(usernameField);
            panel.remove(passwordField);

            setButton("maintenance button.png","maintenance button selected.png",81,209);
            setButton("summary button.png","summary button selected.png",206,209);
            setButton("billing button.png","billing button selected.png",331,209);
            setButton("user button.png","user button selected.png",456,209);

            setButton("eir out button.png","eir out button selected.png",81,335);
            setButton("eir in button.png","eir in button selected.png",206,335);
            setButton("outstanding chassis client button.png","outstanding chassis client button selected.png",331,335);
            setButton("outstanding chassis button.png","outstanding chassis button selected.png",456,335);

            buttonListeners();

            logout.setLocation(500,510);
            logout.setSize(80,25);
            logout.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                        Login out = new Login();
                        i = 0;
                        dispose();
                }
            });

            panel.add(logout,0,1);
            disableButtons();
            enableButtons();

        }
    }
  
    public void login(){
        
        Font font = new Font("Arial", Font.PLAIN, 18);

        login.setText("Login");


        login.setLocation(280,364);


        login.setSize(80,25);

        cancel.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        
        login.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                checkUser();
               
            }
        });
        
    

        username.setText("Username:");
        password.setText("Password:");

        username.setLocation(181, 309);
        password.setLocation(184, 334);

        usernameField.setLocation(281,307);
        passwordField.setLocation(281,332);

        usernameField.setSize(170, 25);
        passwordField.setSize(170, 25);

        username.setSize(90, 20);
        password.setSize(90, 20);

        username.setFont(font);
        password.setFont(font);

        panel.add(username, 1,1);
        panel.add(password, 1,1);
        panel.add(usernameField, 1,1);
        panel.add(passwordField, 1,1);
        panel.add(login,1,1);
        panel.add(cancel,1,1);

        //Natalie
        pl = new JPanel();
        pl.setOpaque(false);
        pl.setLocation(613, 165);
        pl.setSize(287, 340);
        panel.add(pl, 1, 1);

    }


    public void initText(){

        description.setLocation(80,470);
        description.setSize(500,50);
        description.setText("");
        panel.add(description,0,1);

    }

    public void buttonListeners(){
        button[0].addMouseListener(new MouseListener(){

            public void mouseClicked(MouseEvent e) {

                
            }

            public void mousePressed(MouseEvent e) {

            }

            public void mouseReleased(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {
                description.setText("This Menu is for all the Maintenance tools");
            }

            public void mouseExited(MouseEvent e) {
                description.setText("");
            }
        });

        button[0].addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                panel.remove(pl);
                repaint();
                pl = new MaintenancePanel();
                panel.add(pl, 1, 1);
            }
        });

        button[1].addMouseListener(new MouseListener(){

            public void mouseClicked(MouseEvent e) {
                
            }

            public void mousePressed(MouseEvent e) {

            }

            public void mouseReleased(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {
                description.setText("This is a Summary of all EIR's in the range of the given dates");
            }

            public void mouseExited(MouseEvent e) {
                description.setText("");
            }

            public void actionPerformed(ActionEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });

        button[1].addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                panel.remove(pl);
                repaint();
                pl = new SummaryPanel();
                panel.add(pl, 1, 1);

            }
        });

        button[2].addMouseListener(new MouseListener(){

            public void mouseClicked(MouseEvent e) {

            }

            public void mousePressed(MouseEvent e) {

            }

            public void mouseReleased(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {
                description.setText("This is for the billing module.");
            }

            public void mouseExited(MouseEvent e) {
                description.setText("");
            }
        });

        button[2].addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                panel.remove(pl);
                repaint();
                pl = new BillingPanel();
                panel.add(pl, 1, 1);
            }
        });

        button[3].addMouseListener(new MouseListener(){

            public void mouseClicked(MouseEvent e) {

            }

            public void mousePressed(MouseEvent e) {

            }

            public void mouseReleased(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {
                description.setText("This is a for the user profile module");
            }

            public void mouseExited(MouseEvent e) {
                description.setText("");
            }
        });

        button[3].addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {

                UserProfile user = new UserProfile();

                panel.remove(pl);
                disableButtons();
                pl = new Lightbox(user);
                panel.add(pl, 1, 1);


                NButton close = new NButton("Close",410,420);
                user.add(close,0);
                close.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {
                    panel.remove(pl);
                    repaint();
                    enableButtons();
                    }
                });
            }
        });

        button[4].addMouseListener(new MouseListener(){

            public void mouseClicked(MouseEvent e) {

            }

            public void mousePressed(MouseEvent e) {

            }

            public void mouseReleased(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {
                description.setText("This is for adding a new EIR-OUT");
            }

            public void mouseExited(MouseEvent e) {
                description.setText("");
            }
        });

        button[4].addActionListener(new ActionListener(){


            public void actionPerformed(ActionEvent e) {

                EiroutPanel eirout = new EiroutPanel();

                panel.remove(pl);
                disableButtons();
                pl = new Lightbox(eirout);
                panel.add(pl, 1, 1);


                NButton cancel = new NButton("Cancel",410,420);
                NButton ok = new NButton("Ok",320 ,420);
                eirout.add(ok,0);
                eirout.add(cancel,0);
                cancel.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {
                    panel.remove(pl);
                    repaint();
                    enableButtons();
                    }
                });

                ok.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {
                    EiroutPanel.addEirOut();
                    panel.remove(pl);
                    repaint();
                    enableButtons();
                    //FormWarning warn = new FormWarning("incorrect");
                    }
                });
            }
        });

        button[5].addMouseListener(new MouseListener(){

            public void mouseClicked(MouseEvent e) {

                

            }

            public void mousePressed(MouseEvent e) {

            }

            public void mouseReleased(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {
                description.setText("This is for adding a new EIR-IN");
            }

            public void mouseExited(MouseEvent e) {
                description.setText("");
            }
        });

        button[5].addActionListener(new ActionListener(){


            public void actionPerformed(ActionEvent e) {

                EirinPanel eirin = new EirinPanel();

                panel.remove(pl);
                disableButtons();
                pl = new Lightbox(eirin);
                panel.add(pl, 1, 1);


                NButton cancel = new NButton("Cancel",410,420);
                NButton ok = new NButton("Ok",320 ,420);
                eirin.add(ok,0);
                eirin.add(cancel,0);
                cancel.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {
                    panel.remove(pl);
                    repaint();
                    enableButtons();
                    }
                });

                ok.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {
                    EirinPanel.addEirIn();
                    panel.remove(pl);
                    repaint();
                    enableButtons();
                    //FormWarning warn = new FormWarning("incomplete");
                    }
                });
            }
        });

        button[6].addMouseListener(new MouseListener(){

            public void mouseClicked(MouseEvent e) {

            }

            public void mousePressed(MouseEvent e) {

            }

            public void mouseReleased(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {
                description.setText("This is a summary of all the transactions made with the given client");
            }

            public void mouseExited(MouseEvent e) {
                description.setText("");
            }
        });

        button[6].addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {

                panel.remove(pl);
                repaint();
                pl = new OCPanel();
                panel.add(pl, 1, 1);

            }
        });

        button[7].addMouseListener(new MouseListener(){

            public void mouseClicked(MouseEvent e) {

            }

            public void mousePressed(MouseEvent e) {

            }

            public void mouseReleased(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {
                description.setText("This is a summary of all the transactions made in the range of the given dates");
            }

            public void mouseExited(MouseEvent e) {
                description.setText("");
            }
        });

        button[7].addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                panel.remove(pl);
                repaint();
                pl = new OCAllPanel();
                panel.add(pl, 1, 1);
          
            }
        });


    } 

    public void disableButtons(){


        for(int i=0;i<8;i++)
            button[i].setEnabled(false);

    }

    public void enableButtons(){

        if(accessType == 0)
            for(int i=0;i<8;i++)
                button[i].setEnabled(true);
        else if(accessType == 1){
            for(int i=0;i<8;i++)
                if(i != 2 && i != 3 && i != 6 && i != 7)
                button[i].setEnabled(true);
        }
        else if (accessType == 2){
            for(int i=0;i<8;i++)
                if(i != 4 && i != 5 && i != 6 && i != 3 && i != 6 && i != 7)
                button[i].setEnabled(true);
        }

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 570, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 268, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Login().setVisible(true);
            }
        });
    }
    
    public void checkUser(){
       
        String user;
        String pass;
        
        user = usernameField.getText();
        pass = passwordField.getText();
        

        try {
            String query = "SELECT * FROM User WHERE userName = '"+user+"' and userPassword = '"+pass+"'";

            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(query);

                if(rs.next()) {
                    String aType = rs.getString("userAccess");
                    int aT = Integer.parseInt(aType);
                    accessType = aT;
                    loginSuccessful();

                } else {
                    description.setText("Either Username or Password is incorrect!");
                    usernameField.setText("");
                    passwordField.setText("");
                }

            Sql.closeSqlConnection(connection);

        } catch (Exception ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
    }

    public void centerScreen(){

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    // Determine the new location of the window
    int w = getSize().width;
    int h = getSize().height;
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;

    // Move the window
    setLocation(x, y);
    }

    public static void setDescription(String s)
    {
        description.setText(s);
    }

    public int getAType(){
        return accessType;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
